var util = require("../../utils/util")
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    totalSale:0,//业绩
    totalAmount:0,
    Y:'',
    M:'',
    list:[
      // {
      //   "id": 1000,
      //   "moths": 4,
      //   "productNum": 3,
      //   "sale": 111,
      //   "salesmanCode": "string",
      //   "salesmanName": "string",
      //   "years": 2018
      // },{
      //   "id": 1000,
      //   "moths": 4,
      //   "productNum": 3,
      //   "sale": 111,
      //   "salesmanCode": "string",
      //   "salesmanName": "string",
      //   "years": 2018
      // }
    ],
    month_a:null,
    month_b:null,
    // show:[false,false],//控制下拉列表的显示隐藏，false隐藏、true显示
    // selectData_a:[],// 年
    // selectData_b:[12,11,10,9,8,7,6,5,4,3,2,1],
    // index:[0,0]
  },
  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value);
    if (e.currentTarget.dataset.i == 'b' && this.data.month_a > e.detail.value || e.currentTarget.dataset.i == 'a' && this.data.month_b < e.detail.value){
      app.titleToast("结束时间大于起始时间~");
      return;
    }
    this.setData({
      ["month_"+e.currentTarget.dataset.i]: e.detail.value
    })
  },
  // // 点击下拉显示框
  // selectTap(e){
  //   let select = e.currentTarget.dataset.i
  //   console.log(select)
  //   var toDo = "show["+select+"]";
  //   this.setData({
  //     [toDo]: !this.data.show[select]
  //   });
  // },
  // // 点击下拉列表
  // optionTap(e){
  //   let select = e.currentTarget.dataset.select//第几个选择器 0，1
  //   let index=e.currentTarget.dataset.index;//获取点击的下拉列表的下标
  //   console.log("第"+select+"个selector,第"+index+"个option")
  //   this.setData({
  //     ['index['+select+']']:index,
  //     ['show['+select+']']:!this.data.show[select]
  //   });
  // },
  bindSearch:function(){
    let that = this
    let month_s = that.data.month_a;
    let month_e = that.data.month_b;
    if (!that.data.month_a || !that.data.month_b){
      app.titleToast("请选择查询的起止时间");
      return;
    }
    wx.showLoading({
      title:"正在搜索",
      mask:true
    })
    let startDt = month_s+'-01'
    let endTm = month_e.split('-');
    let endDay = new Date(endTm[0],endTm[1],0).getDate();
    let endDt = month_e+'-'+endDay
    console.log('开始时间-'+startDt)
    console.log('结束时间-'+endDt)
      util.post({
        url:"https://groomtea.icebartech.com/api/miniApp/order/monthlyPerformance",
        data:{
          startTime: startDt,
          endTime: endDt
        },
        success:(res)=>{
          wx.hideLoading()
          console.log(res)
          if(res.data.bussData.length == 0){
            wx.showToast({
              title: '此期间没有业绩记录~',
              icon:"none",
              duration:2000
            })
            this.setData({
              list:[]
            })
            return;
          }
          res.data.bussData.map((item,index)=>{
            console.log(item)
            item.gmtCreated = item.gmtCreated.substr(0,7);
          })
          this.setData({
            list:res.data.bussData
          })
        },
        fail:(err)=>{
          console.log(err)
          wx.hideLoading()
          app.titleToast("请求失败，请联系管理人员")
        }
      });
  },
  computeData:function(){
    let that = this
    let amount = 0;
    let sale = 0;
    that.data.list.forEach(element => {
      amount += element.productNum
      sale += element.sale
    });
    that.setData({
      totalAmount:amount,
      totalSale:sale
    })
  },
  onShow: function () {
    let that = this
    //获取本月业绩
    util.post({
      url:"https://groomtea.icebartech.com/api/miniApp/order/currentMonthPerformance",
      success:(res)=>{
        console.log(res)
        that.setData({curMonth:res.data.bussData|| [] })
      }
    })
    
    var timestamp = Date.parse(new Date());
    var date = new Date(timestamp);
    //获取年份  
    var Y = date.getFullYear();
    var M = date.getMonth()+1;
    var D = date.getDate();
    M = M<10?'0'+M:M
    D = D<10?'0'+D:D
    this.setData({Y:Y,M:M,D:D,month_b:Y+"-"+M})//暂时设置近3年为仅有选项
    // this.setData({selectData_a:[Y]})//设1年，查询接口无年份
    this.computeData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})